﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace genericity.实战
{
    /*
     订单状态
     */
    public enum OrderStateEnum
    {
        [Description("待支付")]
        WaitPay,
        [Description("待发货")]
        WaitSend,
        [Description("待收货")]
        WaitReceive,
        [Description("待评论")]
        WaitComment,
        [Description("已完成")]
        Finish,
        [Description("已取消")]
        Cancel
    }
    // 扩展类
    public static class EnumHelper
    {
        // 扩展方法
        public static string ToDescription<T>(this T t)
        {
            Type type = typeof(T);
            // 获取字段
            var field = type.GetField(t.ToString());
            // 获取自定义特性字段
            DescriptionAttribute? desc = field.GetCustomAttribute<DescriptionAttribute>();
            return desc.Description;
        }

        public static Dictionary<int, string> ToDictionaryDescription<T>() { 
            Type type = typeof(T);
            // 创建字典
            Dictionary<int ,string> dic = new Dictionary<int, string>();
            // 获取字段
            var fields = type.GetFields().Skip(1);
            int index = 0;
            foreach (var field in fields) {
                var name = field.GetCustomAttribute<DescriptionAttribute>().Description;
                dic.Add(++index, name);
            }
            return dic;
        }
    }
}
